// pages/education/index.js
import DB from '../../utils/db';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      schoolText: '',
      enrolText: '',
      graduateText: '',
      majorText: '',
      educationText: '',
      subject: '',
      openId: ''
    },
    count: 0,
    educationValue: [],
    education: [
      { label: '专科', value: '专科' },
      { label: '本科', value: '本科' },
      { label: '硕士', value: '硕士' },
      { label: '博士', value: '博士' },
    ],
    mode: '',
    // dateVisible: false,
    enrol: '2018-01', // 支持时间戳传入
    graduate: '2018-01', // 支持时间戳传入
    // 指定选择区间起始值
    start: '1950-01-01 00:00:00',
    end: '2030-09-09 12:12:12',
    confirmBtn: { content: '确定', variant: 'base' },
    dialogKey: '',
    showConfirm: false,
    showWarnConfirm: false,
    showLightConfirm: false,
  },

  showDialog(e) {
    const { key } = e.currentTarget.dataset;
    this.setData({ [key]: true, dialogKey: key });
  },

  closeDialog() {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
    console.log("确认删除")
  },


  onColumnChange(e) {
    console.log('picker pick:', e);
  },

  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;
    console.log('value',value.join(' '))

    // console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
    });
  },

  onPickerCancel(e) {
    const { key } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  onSeasonPicker() {
    this.setData({ dateVisible: true });
    console.log(this.data.dateText)
  },

  onEducationPicker(){
    this.setData({ educationVisible: true });
  },

  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`form.${key}Text`]: value.join(' '),
    });
  },

  onPickerCancel(e) {
    const { key } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  showPicker(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },

  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const { value } = e.detail;
    const { mode } = this.data;

    console.log('confirm', value);

    this.setData({
      [mode]: value,
      [`form.${mode}Text`]: value,
    });

    this.hidePicker();
  },

  onColumnChange(e) {
    console.log('pick', e.detail.value);
  },

  onSchoolTextInput(e){
    // console.log(e.detail.value)
    this.setData({
      'form.schoolText': e.detail.value
    })
  },

  onMajorTextInput(e){
    // console.log(e.detail.value)
    this.setData({
      'form.majorText': e.detail.value
    })
  },

  onSubjectInput(e){
    // console.log(e.detail.value)
    this.setData({
      'form.subject': e.detail.value
    })
  },

  updateEducation(){
    wx.showLoading({
      title: '',
    });
    DB.update('updateEducation', this.data.form).then(() => {
          wx.hideLoading();
          wx.reLaunch({
            url: `/pages/createResume/index`,
          });
        }).catch(() => {
          wx.hideLoading();
          wx.showToast({
            title: '更新失败',
            icon: 'error',
            duration: 2000
          });
        });
  },

  addEducation(){
    // console.log(this.data.openId)
    this.data.form.openId = this.data.openId
    // console.log(this.data.form)
    wx.showLoading({
      title: '',
    });
     DB.add('addEducation', this.data.form).then(() => {
          wx.hideLoading();
          wx.reLaunch({
            url: `/pages/createResume/index`,
          });
        }).catch(() => {
          wx.hideLoading();
          wx.showToast({
            title: '添加失败',
            icon: 'error',
            duration: 2000
          });
        });
  },

  selectEducation(id) {
    DB.select('selectEducation', id).then((resp) => {
          
          this.setData({
            form: resp.result.data,
            count: 1
          });
          wx.hideLoading();
        }).catch(() => {
          console.log(e);
          this.setData({
            showUploadTip: true
          });
          wx.hideLoading();
        });
   
  },
  deleteEducation(){
    wx.showLoading({
      title: '',
    });
    DB.delete('deleteEducation', this.data.form).then(() => {
          wx.hideLoading();
          wx.reLaunch({
            url: `/pages/createResume/index`,
          });
        }).catch(() => {
          wx.hideLoading();
          wx.showToast({
            title: '删除失败',
            icon: 'error',
            duration: 2000
          });
        });
    
  },

  handleChange(e) {
    if(e.detail.value){
      this.setData({
        'form.graduateText': '至今',
      });
    }
    this.setData({
      defaultVal: e.detail.value,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options.id)
    this.setData({
      openId: wx.getStorageSync("openId")
    })
    if(options.id != 'undefined'){
      wx.showLoading({
        title: '数据加载中...', // 这里设置提示词
        mask: true // 显示透明蒙层
      });
      this.selectEducation(options.id)
    }
    // console.log(this.data.count)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})